import 'package:fish_redux/fish_redux.dart';
import 'package:multi_image_picker/multi_image_picker.dart';

import 'action.dart';
import 'state.dart';

Reducer<CameraState> buildReducer() {
  return asReducer(
    <Object, Reducer<CameraState>>{
      CameraAction.init: _init,
      CameraAction.tmpPhoto: _tmpPhoto,
      CameraAction.reject: _reject,
      CameraAction.save: _save,
      // CameraAction.select: _selected,
    },
  );
}

CameraState _init(CameraState state, Action action) {
  final CameraState newState = state.clone()
    ..camera = action.payload['camera']
    ..controller = action.payload['controller'];
  return newState;
}

CameraState _tmpPhoto(CameraState state, Action action) {
  final CameraState newState = state.clone()..tmpPhoto = action.payload;
  return newState;
}

CameraState _reject(CameraState state, Action action) {
  final CameraState newState = state.clone()..tmpPhoto = null;
  return newState;
}

CameraState _save(CameraState state, Action action) {
  String filePath = action.payload;
  final CameraState newState = state.clone();
  print('------------------$filePath');
  newState.tmpPhoto = null;
  return newState;
}

CameraState _selected(CameraState state, Action action) {
  final CameraState newState = state.clone()..selected = action.payload;
  newState.selected.forEach((p) {
    print('------------------selected:${p.identifier}');
    print('------------------selected:${p.name}');
    print('------------------selected:${p.originalWidth}');
    print('------------------selected:${p.originalHeight}');
  });
  return newState;
}
